package com.solution.admin.product.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.product.entity.ProductCategoryBrandEntity;
import com.solution.admin.product.service.ProductCategoryBrandService;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 商品类型关联品牌web控制层
 * @author llp
 * @date 2017-10-10 19:16:54
 */
@RequestMapping("/productCategoryBrand")
@Controller
public class ProductCategoryBrandController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(ProductCategoryBrandController.class);

	@Autowired
	private ProductCategoryBrandService productCategoryBrandService;
	
	/**
	 * 跳转商品类型关联品牌查询页面
	 * @param ProductCategoryBrand 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author llp
 	 * @date 2017-10-10 19:16:54
	 */
	@RequestMapping("/toListProductCategoryBrandPage")
	public String toListProductCategoryBrandPage(ProductCategoryBrandEntity productCategoryBrand, HttpServletRequest request) {
		return "product/productCategoryBrand_list";
	}

	/**
	 * 分页查询商品类型关联品牌列表
	 * @param productCategoryBrand 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author llp
 	 * @date 2017-10-10 19:16:54
	 */
	@RequestMapping("/listProductCategoryBrandPage")
	@ResponseBody
	public JSONGrid listProductCategoryBrandPage(ProductCategoryBrandEntity productCategoryBrand, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("productCategoryId", productCategoryBrand.getProductCategoryId());
		try {
			PageBean pageBean = productCategoryBrandService.listProductCategoryBrandPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询商品类型关联品牌失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author llp
 	 * @date 2017-10-10 19:16:54
	 */
	@RequestMapping("/toAddProductCategoryBrand")
	public String toAddProductCategoryBrand(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "product/productCategoryBrand_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author llp
 	 * @date 2017-10-10 19:16:54
	 */
	@RequestMapping("/toUpdateProductCategoryBrand")
	public String toUpdateProductCategoryBrand(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			ProductCategoryBrandEntity productCategoryBrand = productCategoryBrandService.getProductCategoryBrandById(id);
			request.setAttribute("productCategoryBrand", productCategoryBrand);
		} catch (Exception e) {
			handleException("跳转编辑商品类型关联品牌页面错误", logger, e);
		}
		return "product/productCategoryBrand_form";
	}


	/**
	 * 新增或保存商品类型关联品牌
	 * @param request 请求对象
	 * @param productCategoryBrandEntity 商品类型关联品牌实体
	 * @return
	 * @author llp
 	 * @date 2017-10-10 19:16:54
	 */
	@RequestMapping("/saveOrUpdateProductCategoryBrand")
	@ResponseBody
	public Map<String, Object> saveProductCategoryBrand(HttpServletRequest request, ProductCategoryBrandEntity productCategoryBrandEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				result = productCategoryBrandService.updateProductCategoryBrand(productCategoryBrandEntity);
			}else{
				productCategoryBrandEntity.setCreater(this.getUserName());
				result = (int)productCategoryBrandService.saveProductCategoryBrand(productCategoryBrandEntity);
				resultMap.put("id", result);
			}
		} catch (Exception e) {
			handleException("添加商品类型关联品牌失败", logger, e);
		}
		resultMap.put("code", result);
		return resultMap;
	}
	
	@RequestMapping("/removeProductCategoryBrand")
	@ResponseBody
	public Map<String, Object> removeProductCategoryBrand(HttpServletRequest request, ProductCategoryBrandEntity productCategoryBrandEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			result = productCategoryBrandService.deleteProductCategoryBrandById(productCategoryBrandEntity.getId());
		} catch (Exception e) {
			handleException("添加商品类型关联品牌失败", logger, e);
		}
		resultMap.put("code", result);
		return resultMap;
	}
	
}
